package com.qingke.stockdata.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingke.stockdata.exception.InvalidStockCodeException;
import com.qingke.stockdata.exception.NoResultException;
import com.qingke.stockdata.mappers.StockDao;
import com.qingke.stockdata.model.Stock;
import com.qingke.stockdata.model.StockData;
import com.qingke.stockdata.model.StockResult;

@Service
public class StockServiceImpl implements StockService {

	@Autowired
	private DataCrawler dataCrawler;

	@Autowired
	private StockDao stockDao;

	@Autowired
	private StockResult result;

	public StockResult addStock(String code) {
		Stock stock = null;
		try {
			stock = stockDao.selectStockByCode(code);
		} catch (NoResultException e1) {
			e1.printStackTrace();
		}
		if (stock == null) {// 数据库中不存在股票，则返回true
			try {
				// 获取stock基本信息
				stock = dataCrawler.getStock(code);
				if(stock.getName()==null){// 没有返回stock,则不执行下面的语句
					result.setState("没有找到股票！");
					return null;
				}
				// 获取历史数据并保存
				List<StockData> stockDatas = dataCrawler.getStockHist(code);
				result.setState("恭喜您，股票添加成功！");
				result.setStock(stock);
				result.setStockDatas(stockDatas);
			} catch (InvalidStockCodeException e) {
				e.printStackTrace();
			}
		} else {// 股票已存在数据库中
			result.setState("您已添加了这支股票！");
			result.setStock(stock);
			try {
				List<StockData> stockDatas = stockDao.selectAllStockDetail(code, 10, 0);
				result.setStockDatas(stockDatas);
			} catch (NoResultException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public Stock selectStock(String code) {
		Stock stock = null;
		try {
			stock = stockDao.selectStockByCode(code);
		} catch (NoResultException e) {
			e.printStackTrace();
		}
		return stock;
	}

	public List<StockData> selectStockData(String code) {
		List<StockData> list = null;
		try {
			list = stockDao.selectAllStockDetail(code, 10, 0);
		} catch (NoResultException e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Stock> selectAllStock(int userId) {
		try {
			List<Stock> stocks = stockDao.selectStockByUser(userId);
			if (stocks != null) {
				return stocks;
			}
		} catch (NoResultException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void insertData(Stock stock, List<StockData> stockDatas,int userId) {
		// 获取历史数据并保存
		int minIndex = 0;
		int maxIndex = 0;
		// 批量将历史数据插入数据库（直插入前300条，多的对于用户没有多大参考价值）
		stockDao.addStockDetail(stockDatas.subList(0, 300));
		for (int i = 0; i < stockDatas.size(); i++) {
			StockData sd = stockDatas.get(i);
			// 计算历史最低和最高字段，返回页面、插入数据库
			if (sd.getHigh() > stockDatas.get(maxIndex).getHigh()) {
				maxIndex = i;
			}
			if (sd.getLow() < stockDatas.get(minIndex).getLow()) {
				minIndex = i;
			}
		}
		stock.setHistoryHigh(stockDatas.get(maxIndex).getHigh());
		stock.setHisotryLow(stockDatas.get(minIndex).getLow());
		stockDao.addStock(stock);
		int stockId = stock.getId();// 获得插入stock表后的主键stockId
		insertStockUser(userId, stockId);
	}

	public int insertStockUser(int userId, int stockId) {
		stockDao.addUserStock(userId, stockId);
		return 0;
	}
}
